<template>
  <div style="margin: 0 10px;">
    <div style="float:right;margin:5px 0px 5px 5px">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        <el-button type="success" plain icon="el-icon-refresh" size="mini" @click="getList">刷新</el-button>
      </el-col>
    </div>

    <el-table v-loading="loading" :data="misviewList" border>
      <el-table-column label="功能编号" width="80px" align="center" prop="ctFunNo" />
      <el-table-column label="字段标题" header-align="center" align="left" prop="ctTitle" />
      <el-table-column label="字段" header-align="center" align="left" prop="ctName" />
      <el-table-column label="前端格式" align="center" prop="ctContype" />
      <el-table-column label="字段宽度" align="center" prop="ctWide" />
      <el-table-column label="对齐方式" align="center" prop="ctAlign" />
      <el-table-column label="是否显示" align="center" prop="ctIfTable" />
      <el-table-column label="穿透传值" align="center" prop="ctCode" />
      <el-table-column label="排序" align="center" prop="amSortTable" />
      <el-table-column
        width="120px"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="medium"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            title="修改"
          ></el-button>
          <el-button
            size="medium"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            title="删除"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改显示配置对话框  -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-row class="mb8">
        <div style="float:right;">
          <el-button type="primary" size="mini" @click="submitForm" v-if="canSave">保 存</el-button>
        </div>
      </el-row>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="隶属功能" prop="ctFunNo">
              <el-input v-model="form.ctFunNo" placeholder="请输入功能编号" disabled />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="字段名称" prop="ctName">
              <el-input v-model="form.ctName" placeholder="请输入字段名称" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="字段标题" prop="ctTitle">
              <el-input v-model="form.ctTitle" placeholder="请输入字段标题" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="显示格式" prop="ctContype">
              <el-input v-model="form.ctContype" placeholder="显示格式（日期格式）" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="字段宽度" prop="ctWide">
              <el-input v-model="form.ctWide" placeholder="请输入字段宽度,支持百分比和px" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="对齐模式" prop="ctAlign">
              <el-select v-model="form.ctAlign" placeholder="请选择显示模式" style="width:100%;">
                <el-option label="居中" value="center" />
                <el-option label="居左" value="left" />
                <el-option label="居右" value="right" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="穿透传值" prop="ctCode">
              <el-input v-model="form.ctCode" placeholder="输入穿透时的传值" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="状态设置颜色" prop="ctColor">
              <el-input v-model="form.ctColor" placeholder="请输入状态设置颜色" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="是否显示" prop="ctIfTable">
              <el-select v-model="form.ctIfTable" placeholder="请选择是否list显示" style="width:100%;">
                <el-option label="是" value="1" />
                <el-option label="否" value="0" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="排序" prop="amSortTable">
              <el-input v-model="form.amSortTable" placeholder="请输入排序，按照5的倍数输入" />
            </el-form-item>
          </el-col>
          <!-- <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="12">
            <el-form-item label="其他特殊配置" prop="ctOther">
              <el-input v-model="form.ctOther" type="textarea" placeholder="请输入其他特殊配置，显示字体颜色等"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="是否允许排序" prop="ctIfsort">
              <el-select v-model="form.ctIfsort" placeholder="请选择是否允许排序" style="width:100%;">
              <el-option label="是" value="1"/>
              <el-option label="否" value="0"/>
            </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="是否在form构建" prop="ctIfform">
               <el-select v-model="form.ctIfform" placeholder="请选择是否在form构建" style="width:100%;">
              <el-option label="是" value="1"/>
              <el-option label="否" value="0"/>
            </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="form中排列顺序" prop="amSortForm">
              <el-input v-model="form.amSortForm" placeholder="请输入form中排列顺序"/>
            </el-form-item>
          </el-col>-->
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="使用状态" prop="ctState">
              <el-switch active-value="1" inactive-value="9" v-model="form.ctState"></el-switch>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMisview,
  getMisview,
  delMisview,
  addMisview,
  updateMisview,
  exportMisview,
} from '@/api/mis/misview'
import { tabeleColumn } from '@/api/mis/function'
import MisSelectTree from '@/components/SpsPublic/treeview/MisSelectTree'
import store from '@/store/index'
import { getFunNo } from '@/utils/party'

export default {
  name: 'Misview',
  components: {
    MisSelectTree,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 显示配置表格数据
      misviewList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      store: store,
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      tabeleColumn: [],
      singleStore: {},
      subSingleStore: {},
      funNo: getFunNo(this.$route),
    }
  },
  created() {
    this.singleStore = store.getters.singleStore['main' + this.funNo]
    this.subSingleStore = this.singleStore.subSingleStore['sub' + this.subFunNo]
    // this.getTabeleColumn();
    this.getList()
  },

  methods: {
    /** 查询显示配置列表 */
    getList() {
      this.loading = true
      this.queryParams.ctFunNo = this.subSingleStore.ctId
      listMisview(this.queryParams)
        .then((response) => {
          this.misviewList = response.rows
          this.total = response.total
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    },
    //目前未使用，因为获取的table名字不正确， 需要根据funno去查询一次才可以。
    getTabeleColumn() {
      let table = this.singleStore.ctFun.ctView
      if (!table) table = this.singleStore.ctFun.ctTable
      tabeleColumn(table).then((response) => {
        this.tabeleColumn = response.data
      })
    },
    // 表单重置
    reset() {
      this.form = {
        ctId: null,
        ctFunNo: null,
        ctColId: null,
        ctName: null,
        ctTitle: null,
        ctContype: null,
        ctWide: null,
        ctAlign: null,
        ctColor: null,
        ctIfTable: null,
        amSortTable: null,
        ctOther: null,
        ctIfsort: null,
        ctIfform: null,
        amSortForm: null,
        ctState: null,
      }
      this.resetForm('form')
      this.getTabeleColumn()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.title = '添加显示配置'
      this.reset()
      this.$nextTick(() => {
        this.form.ctFunNo = this.subFunNo
        this.form.ctState = '1'
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const ctId = row.ctId || this.ids
      getMisview(ctId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改显示配置'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.ctId != null) {
            updateMisview(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addMisview(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ctIds = row.ctId || this.ids
      this.$confirm(
        '是否确认删除显示配置编号为"' + ctIds + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      )
        .then(function () {
          return delMisview(ctIds)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
    },
  },
}
</script>
